package com.xiejr.actuator.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiejr.actuator.entity.JvmInfo;
import com.xiejr.actuator.enums.JvmEnum;
import com.xiejr.actuator.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import store.sophi.xjr.enums.base.ImageTypeEnum;
import store.sophi.xjr.enums.base.LivenessControlEnum;
import store.sophi.xjr.enums.check.FaceFieldEnum;
import store.sophi.xjr.enums.check.FaceNumEnum;
import store.sophi.xjr.enums.check.FaceTypeEnum;
import store.sophi.xjr.util.AiFaceTemplate;

import java.lang.management.ManagementFactory;

/**
 * @program: socket-actuator-spring-boot-starter
 * @description: 系统服务实现
 * @author: xjr
 * @create: 2020-07-02 10:19
 **/
@Service("SystemService")
@Lazy
public class SystemServiceImpl implements SystemService {

    @Autowired
    AiFaceTemplate aiFaceTemplate;

    @Override
    public JvmInfo getRuntimeInfo() {
        JSONObject operatingSystem= JSON.parseObject(JSON.toJSONString(ManagementFactory.getOperatingSystemMXBean()));
        JvmInfo jvmInfo=JvmInfo.builder().cpuNums(operatingSystem.getInteger(JvmEnum.AVAILABLE_PROCESSORS.getCode()))
                .cpuUseAge(operatingSystem.getFloat(JvmEnum.SYSTEM_CPU_LOAD.getCode()))
                .memoryFreeBytes(operatingSystem.getLong(JvmEnum.FREE_PHYSICAL_MEMORY_SIZE.getCode()))
                .memoryTotalBytes(operatingSystem.getLong(JvmEnum.TOTALP_HYSICAL_MEMORYS_IZE.getCode()))
                .osName(operatingSystem.getString(JvmEnum.OS_NAME.getCode())).build();
        return jvmInfo;
    }

    @Override
    public String aiCheck(String base64Img) throws Exception {
        return aiFaceTemplate.checkFace(base64Img, ImageTypeEnum.BASE64, FaceNumEnum.ONE, FaceTypeEnum.LIVE, LivenessControlEnum.NORMAL, FaceFieldEnum.AGE,FaceFieldEnum.GENDER);
    }

    @Override
    public String aiSearch(String base64Img) throws Exception {
        return aiFaceTemplate.search(base64Img,ImageTypeEnum.BASE64,1,80,LivenessControlEnum.NORMAL,LivenessControlEnum.NORMAL,1,"admin");
    }
}
